package 链表;

import common.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * ---------------------------------------------------------
 * <h></h>
 * <p></p>
 * Created by Frank on 2021/3/29.
 * <a href="mailto:frankyao10110@gmail.com">Contact me</a>
 * ---------------------------------------------------------
 */
public class _反转链表 {

    public static void main(String[] args) {
        ListNode list = new ListNode(1);
        list.next = new ListNode(3);
        list.next.next = new ListNode(6);
        list.next.next.next = new ListNode(2);

        ListNode rvList = reverseList(list);
        while (rvList != null) {
            System.out.println("reverse: " + rvList.val);
            rvList = rvList.next;
        }
    }

    public static ListNode reverseList(ListNode head) {

        ListNode newHead = null;

        while (head != null) {
            ListNode next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }


        return newHead;
    }
}
